import 'dart:io';
import 'package:crop_your_image/crop_your_image.dart';
import 'package:flutter/material.dart';

// import 'package:image_crop/image_crop.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';

/// 裁剪的宽高做成可配置功能 @yetian
class CropImageRoute extends StatefulWidget {
  final File image; //原始图片路径

  CropImageRoute(this.image);

  @override
  _CropImageRouteState createState() => new _CropImageRouteState();
}

class _CropImageRouteState extends State<CropImageRoute> {
  late double baseLeft; //图片左上角的x坐标
  late double baseTop; //图片左上角的y坐标
  late double imageWidth; //图片宽度，缩放后会变化
  double imageScale = 1; //图片缩放比例
  late Image imageView;

  // final cropKey = GlobalKey<CropState>();
  CropController cropController = CropController();

  // Uint8List =
  late Future _future;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _future = widget.image.readAsBytes();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: ThemeColor.bkColorFFFFFF,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Container(
                height: 400,
                // child: Crop.file(
                //   widget.image,
                //   key: cropKey,
                //   aspectRatio: 1.0,
                //   alwaysShowGrid: true,
                // ),
                child: FutureBuilder(
                    future: _future,
                    builder: (context, snap) {
                      if (snap.hasData) {
                        return Crop(
                            image: snap.data,
                            aspectRatio: 1,
                            controller: cropController,
                            onCropped: (CropResult result) {
                              DLog('dasldj  ${result.toString()}');
                              // switch(result) {
                              // case CropResu.success(:final croppedImage):
                              // do something with cropped image data
                              // case CropResult.error(:final error):
                              // do something with error
                              // }
                            });
                      } else {
                        return const SizedBox();
                      }
                    })),
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  TextButton(
                    onPressed: ClickUtils.debounce(() {
                      Navigator.pop(context);
                    }),
                    child: Text(
                      S.of(context).cancel,
                      style: TextStyle(color: ThemeColor.colorBlack100),
                    ),
                    // color: Colors.white,
                  ),
                  TextButton(
                    onPressed: ClickUtils.debounce(() {
                      _doCrop(widget.image);
                    }),
                    child: Text(
                      S.of(context).confirm,
                      style: TextStyle(color: ThemeColor.colorBlack100),
                    ),
                    // color: Colors.white,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Future<void> _doCrop(File originalFile) async {
    cropController.crop();
    // final crop = cropKey.currentState;
    // final area = crop!.area;
    // if (area == null) {
    //   //裁剪结果为空
    //   print('裁剪不成功');
    //   return;
    // }
    // bool hasPermissions = await ImageCrop.requestPermissions();
    // if (hasPermissions) {
    //   File cropedFile =
    //       await ImageCrop.cropImage(file: originalFile, area: area);
    //   Navigator.pop(context, cropedFile);
    //   return;
    // }

    // print('request permissions failed, return original image file.');
    Navigator.pop(context, originalFile);
  }
}
